<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Non-logical Variables
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot053.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot049.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot055.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc131">9.5</A>&nbsp;&nbsp;Non-logical Variables</H2> 
<A NAME="@default465"></A>
Non-logical variables in ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> are a means of storing a copy
of a Prolog term under a name (an atom).
The atom is the <I>name</I> and the associated
term is the <I>value</I> of the non-logical variable.
This term may be of any form, whether an integer or a huge compound
structure.
Note that the associated term is being copied and so if it is not ground,
the retrieved term is not strictly identical to the stored one
but is a <I>variant</I> of it<SUP><A NAME="text12" HREF="umsroot049.html#note12">1</A></SUP>.
There are two fundamental operations that can be performed on a non-logical variable:
setting the variable (giving it a value), and referencing the variable
(finding the value currently associated with it).<BR>
<BR>
The value of a non-logical variable is set using the <A HREF="../bips/kernel/storage/setval-2.html"><B>setval/2</B></A><A NAME="@default467"></A> predicate.
<A NAME="@default468"></A>
This has the format
<BLOCKQUOTE CLASS="quote">
<B>setval(Name, Value)</B>
</BLOCKQUOTE>
For instance, the goal <BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
setval(firm, 3)</PRE></BLOCKQUOTE>
gives the non-logical variable <I>firm</I> the value 3.
The value of a non-logical variable is retrieved using the <A HREF="../bips/kernel/storage/getval-2.html"><B>getval/2</B></A><A NAME="@default469"></A> predicate.
<A NAME="@default470"></A>
The goal <BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
getval(firm, X)</PRE></BLOCKQUOTE>
will unify <I>X</I> to
the value of the non-logical variable <I>firm</I>, which has been previously set by
<A HREF="../bips/kernel/storage/setval-2.html"><B>setval/2</B></A><A NAME="@default471"></A>.
If no value has been previously set, the call raises an exception.
If the value of a non-logical variable is an integer, the predicates <A HREF="../bips/kernel/storage/incval-1.html"><B>incval/1</B></A><A NAME="@default472"></A>
and <A HREF="../bips/kernel/storage/decval-1.html"><B>decval/1</B></A><A NAME="@default473"></A> may be used to increment and decrement the value of the
<A NAME="@default474"></A>
<A NAME="@default475"></A>
variable, respectively.
The predicates <A HREF="../bips/kernel/storage/incval-1.html"><B>incval/1</B></A><A NAME="@default476"></A> and
<A HREF="../bips/kernel/storage/decval-1.html"><B>decval/1</B></A><A NAME="@default477"></A> may be used e.g. in a failure-driven loop to provide
an incremental count across failures as in the example:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
count_solutions(Goal, _) :-
        setval(count, 0),
        call(Goal),
        incval(count),
        fail.
count_solutions(_, N) :-
        getval(count, N).
</PRE></BLOCKQUOTE>
However, code like this should be used carefully.
Apart from being a non-logical feature, it also causes the code to be
not reentrant. I.e. if count_solutions/2 would be called recursively from
inside <TT>Goal</TT>, this would smash the counter and yield incorrect
results<SUP><A NAME="text13" HREF="umsroot049.html#note13">2</A></SUP>.<BR>
<BR>
The visibility of a non-logical variable is local to the module
where it is first set. It is good style to declare them using
<A HREF="../bips/kernel/modules/local-1.html"><B>local/1</B></A><A NAME="@default478"></A>
<A HREF="../bips/kernel/storage/variable-1.html"><B>variable/1</B></A><A NAME="@default479"></A>
declarations. E.g. in the above example one should use
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
:- local variable(count).
</PRE></BLOCKQUOTE>
If it is necessary to access the value of a variable in other modules,
exported access predicates should be provided.<BR>
<BR>
<HR>
<A HREF="umsroot053.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot049.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot055.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
